<template>
	<view class="">
		<image src="/static/images/logo.png" class="login_logo"></image>
		<view class="login_item">
			<image src="/static/images/login_icon001.png" mode="aspectFit"></image>
			<input type="text" placeholder="请输入账号" v-model="upJson.account" />
		</view>

		<view class="login_item">
			<image src="/static/images/login_icon002.png" mode="aspectFit"></image>
			<input type="password" placeholder="请输入密码" v-model="upJson.password" />
		</view>

		<button open-type="getUserInfo" @getuserinfo="bindgetuserinfo" class="save" v-if="!loginStatus">授权登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				upJson: {
					"account": "",
					"password": "",
				},
				loginStatus: false,
				code: "", //微信code
			}
		},
		onLoad(options) {
			this.upJson.account = uni.getStorageSync("account");
			this.upJson.password = uni.getStorageSync("password");
			if (options.msg) {
				uni.showToast({
					title: options.msg,
					icon: "none"
				})
			}
		},
		onShow() {
			uni.hideHomeButton();
		},
		methods: {
			bindgetuserinfo(e) { // 微信登录
				console.log(e)
				let that=this;
				uni.checkSession({
					success(res) {
						if (res.errMsg == "checkSession:ok") {
							that.lodinged();
						} else {
							that.beforeEmpower(e)
						}
					},
					fail(err) {
						that.beforeEmpower(e)
					}
				})
			},
			beforeEmpower(e) {
				if (e.detail.errMsg == "getUserInfo:ok") {
					uni.login({
						provider: "weixin",
						success: res => {
							console.log('登录获取code相关信息：', res)
							this.$api.getWxUserinfo({
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv,
								code: res.code
							}).then(res => {
								this.lodinged();
							})
						}
					})
				}
			},
			lodinged() {
				this.$api.logined(this.upJson).then(res => {
					uni.setStorageSync("account", this.upJson.account)
					uni.setStorageSync("password", this.upJson.password)
					uni.setStorageSync("userId", res.user_id) 
					console.log("登录返回值0-----》》》》",res);
					uni.showToast({
						title: "登录成功",
						icon: "none",
						mask: true,
						duration: 2000,
						success: res => {
							setTimeout(res => {
								uni.reLaunch({
									url: "/pages/index/index"
								})
							}, 2000)
						}
					})
				})
			}
		}
	}
</script>

<style>
	page {
		background: #FFFFFF;
	}

	.login_logo {
		width: 300upx;
		height: 90upx;
		margin: 56upx 0 60upx 228upx;
	}

	.login_item {
		width: 630upx;
		height: 90upx;
		margin: 10upx 0 0 60upx;
		display: flex;
		flex-direction: row;
		align-items: center;
		font-size: 30upx;
		color: #333333;
		border-bottom: 1px solid #D4D4D4;
	}

	.login_item image {
		width: 32upx;
		height: 36upx;
		margin-right: 20upx;
	}

	.login_item input {
		flex-grow: 1;
		font-size: 30upx;
	}

	.save {
		width: 630upx;
		margin-left: 60upx;
		margin-top: 200upx;
	}
</style>
